package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Company;
import net.zjitc.entity.Manager;
import net.zjitc.entity.User;
import net.zjitc.entity.VO.RecruitVo;
import net.zjitc.entity.Yonghu;
import net.zjitc.service.ICompanyService;
import net.zjitc.service.IUserService;
import net.zjitc.until.MD5Utils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @auther 郭梦婷
 * @create 2022-04-27 16:12:28
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "企业管理列表")
public class CompanyController {
    @Reference
    private ICompanyService companyService;
    ResponseResult result=new ResponseResult();

    @Autowired(required=true)
    private MD5Utils md5Utils;

    @Reference
    private IUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @ApiOperation("获取所有企业信息")
    @GetMapping("company")
    public ResponseResult getCompanys(@RequestParam(required = false,value ="querystring" ) String querystring,
                                     @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                     @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<Company> company=companyService.getCompanys(querystring,pagenum,pagesize);
        if (company.getTotal()>0){
            result.setSuccess("获取成功",company);
        }else {
            result.setError("获取失败");
        }
        return result;
    }
    @ApiOperation("根据id查找企业")
    @GetMapping("company/{id}")
    public ResponseResult getCompanyById(@PathVariable("id") Integer id){
        Company company = companyService.getCompanyById(id);

        if (company!=null){
            result.setSuccess("获取企业成功",company);
        }else {
            result.setError("获取企业失败");
        }
        return result;
    }

    @ApiOperation("根据companyid查找企业")
    @GetMapping("getCompanyByCompanyid")
    public ResponseResult getCompanyByCompanyid(@RequestParam(required = false) String companyid){
        Company company = companyService.selectByCompanyid(companyid);
        if (company!=null){
            result.setSuccess("获取企业成功",company);
        }else {
            result.setError("获取企业失败");
        }
        return result;
    }

    @ApiOperation("注册企业")
    @PostMapping("regcompany")
    public ResponseResult AddCompany(@RequestBody Company company){
        User user = userService.findByUsername(company.getCompanyid());
        if (user==null) {
            User user1=new User();
            String pwd = passwordEncoder.encode(company.getPassword());
            Date time = new Date();
            company.setPassword(pwd);
            company.setAddtime(time);
            boolean save = companyService.save(company);

            Company company1= companyService.selectByCompanyid(company.getCompanyid());

            user1.setUsername(company1.getCompanyid());
            user1.setPassword(pwd);
            user1.setActorid(company1.getId());
            user1.setRoleid(3);
            user1.setCreateTime(time);
            boolean save1 = userService.save(user1);


            if (save&&save1){
                result.setCreateSuccess("添加用户成功");
            }
        }else {
            result.setError("账号重复");

        }

        return result;
    }
    @ApiOperation("修改企业")
    @PutMapping("company/{id}")
    public ResponseResult UpdateCompany(@RequestBody Company company,@PathVariable("id") int id){
        company.setId(id);

        if (company.getPlaceids()!=null){
            company.setPlaceid(String.valueOf(company.getPlaceids().get(2)));
        }
        boolean update = companyService.updateById(company);
        if (update){
            result.setSuccess("修改企业成功");
        }else {
            result.setError("修改企业失败");
        }
        return result;
    }
    @ApiOperation("删除企业")
    @DeleteMapping("company/{id}")
    public ResponseResult DeleteCompany(@PathVariable("id") int id){

        int delete= companyService.deleteById(id);
        if (delete!=0){
            result.setSuccess("删除企业成功");
        }else {
            result.setError("删除企业失败");
        }
        return result;
    }
    @ApiOperation("修改用户密码")
    @GetMapping("/updateCompanyPwd")
    public ResponseResult updatePassword(@RequestParam(required = false) String companyid,
                                         @RequestParam(required = false) String pwd1,
                                         @RequestParam(required = false) String pwd2,
                                         @RequestParam(required = false) String pwd3){
        //加解密方法
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        Company company = companyService.selectByCompanyid(companyid);
        User user = userService.findByUsername(companyid);
        //输入密码与数据库密码比较
        boolean matches = bc.matches(pwd1,user.getPassword());
        if (!matches){
            result.setError("旧密码不匹配");
        }else {
            String pwd = passwordEncoder.encode(pwd2);
            user.setPassword(pwd);
            company.setPassword(pwd);
            boolean updateuser = userService.updateById(user);
            boolean updateCompany = companyService.updateById(company);
            if (updateuser&&updateCompany){
                result.setSuccess("修改密码成功");
            }else {
                result.setError("修改密码失败");
            }
        }
        return result;
    }

    @ApiOperation("根据条件查询公司信息")
    @GetMapping("SearchCompany")
    public ResponseResult SearchCompany(@RequestParam(required = false) String querystring,
                                        @RequestParam(required = false) String placeids,
                                        @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                        @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<Company> Company= companyService.SearchCompany(querystring,placeids,pagenum,pagesize);
        if (Company.getTotal()!=0){
            result.setSuccess("获取企业成功",Company);
        }else {
            result.setError("获取企业失败");
        }
        return result;
    }
}

